package com.pb.infra.auth.controller;

import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.entity.common.Tree;
import com.pb.infra.auth.entity.custom.PriviRoleCustom;
import com.pb.infra.auth.entity.pbsys.PriviRole;
import com.pb.infra.auth.service.PriviRoleService;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.StringUtil;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author liudebao
 * @date 2020/1/1
 */
@Slf4j
@RestController
@RequestMapping("v1/system/role")
public class PriviRoleController {

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "privi_role";

    /**
     * 日志操作表
     */
    private static final String TABLE_NAMETWO = "privi_role_menu";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "角色管理";

    /**
     * 业务逻辑对象
     */
    @Resource
    private PriviRoleService roleService;


    /**
     * 查询角色信息
     *
     * @param condition 请求参数
     * @param page
     * @return Result 返回数据
     */
    @GetMapping("select")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result select(PriviRoleCustom condition, Page page, HttpServletRequest request) {
        return roleService.select(condition, page, request);
    }

    /**
     * 新增角色
     *
     * @param priviRole 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(PriviRole priviRole, HttpServletRequest request) {
        Map<String, Object> map1 = roleService.insert(priviRole, request);
        if (map1 != null && !Constant.OK.equals(map1.get("code").toString()) && !StringUtil.isEmpty(map1.get("msg"))) {
            return Result.error(map1.get("code").toString(), map1.get("msg").toString());
        } else {
            return Result.ok(map1);
        }
    }

    /**
     * 修改角色
     *
     * @param priviRole 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(PriviRole priviRole, HttpServletRequest request) {
        Map<String, Object> map1 = roleService.update(priviRole, request);
        if (map1 != null && !Constant.OK.equals(map1.get("code").toString()) && !StringUtil.isEmpty(map1.get("msg"))) {
            return Result.error(map1.get("code").toString(), map1.get("msg").toString());
        } else {
            return Result.ok(map1);
        }
    }

    /**
     * 删除角色
     *
     * @param priviRoleList 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody List<PriviRole> priviRoleList, HttpServletRequest request) {
        roleService.delete(priviRoleList, request);
        return Result.OK_EMPTY;
    }

    /**
     * 读取某角色配置的用户分页信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @GetMapping("getRoleUserListByPage")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getRoleUserListByPage(@RequestParam Map<String, Object> paramMap, Page page) {
        return roleService.getRoleUserListByPage(paramMap, page);
    }

    /**
     * 读取某角色配置的菜单信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @GetMapping("getMenuByRoleId")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getMenuByRoleId(@RequestParam Map<String, Object> paramMap) {
        return roleService.getMenuByRoleId(paramMap);
    }

    /**
     * 保存角色配置的菜单信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("saveMenuAuth")
    @SysLog(tableName = TABLE_NAMETWO, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result saveMenuAuth(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        roleService.saveMenuAuth(paramMap, request);
        return Result.OK_EMPTY;
    }

    /**
     *查询某法人所有角色
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @GetMapping("selAllRole")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selAllRole(@RequestParam Map<String, Object> paramMap) {
        return roleService.selAllRole(paramMap);
    }

    @GetMapping("queryMenuInter")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result queryMenuInter(@RequestParam Map<String, Object> paramMap, Page page) {
        return roleService.queryMenuInter(paramMap, page);
    }

    @GetMapping("queryAllMenu")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result queryAllMenu(String appId) {
        return roleService.queryAllMenu(appId);
    }

    @PostMapping("saveInterAuth")
    @SysLog(tableName = TABLE_NAMETWO, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result saveInterAuth(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        roleService.saveInterAuth(paramMap, request);
        return Result.OK_EMPTY;
    }

    @GetMapping("queryBtnRes")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result queryBtnRes(@RequestParam Map<String, Object> paramMap) {
        return roleService.queryBtnRes(paramMap);
    }

}
